package com.leyou.cart.web;

import com.leyou.cart.entity.Cart;
import com.leyou.cart.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CartController {


    @Autowired
    private CartService cartService;


    /**
     * 添加商品到购物车
     * @param cart
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> addItemToCart(@RequestBody Cart cart){
        cartService.addItemToCart(cart);

        return ResponseEntity.ok().build();
    }

    /**
     * 查询商品购物车
     */
    @GetMapping("list")
    public ResponseEntity<List<Cart>> queryCartList(){

        return ResponseEntity.ok(cartService.queryCartList());
    }

    /**
     * 修改购物车数量
     */
    @PutMapping
    public ResponseEntity<Void> updateCart(@RequestParam("id") Long id,@RequestParam("num") Integer num){

        cartService.updateCart(id,num);
        return ResponseEntity.status(HttpStatus.OK).build();
    }

    /**
     * 删除购物车商品
     */
    @DeleteMapping("{skuId}")
    public ResponseEntity<Void> deleteCart(@PathVariable("skuId") String skuId){

        cartService.deleteCart(skuId);
        return ResponseEntity.status(HttpStatus.OK).build();
    }

    /**
     * 添加未登录商品集合到购物车
     * @param carts
     * @return
     */
    @PostMapping("list")
    public ResponseEntity<Void> addItemToCart(@RequestBody List<Cart> carts){
        cartService.addItemToCarts(carts);

        return ResponseEntity.ok().build();
    }
}
